package com.rightsidetech.scheduler.task;

import com.rightsidetech.activity.service.ActCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Component
@EnableScheduling
public class ActCouponUpdateStatusTask implements SchedulingConfigurer {

    @Resource
    private ActCouponService actCouponService;

    public void updateStatusTask(){
        actCouponService.updateStatusByEndTime();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                new Runnable() {
                    @Override
                    public void run() {
                        updateStatusTask();
                    }
                }, new Trigger() {
                    @Override
                    public Date nextExecutionTime(TriggerContext triggerContext) {
                        //查询任务的执行周期corn表达式.
                        String cornExpression = "0 0 1 * * ?";

                        if (null != cornExpression) {
                            CronTrigger trigger = new CronTrigger(cornExpression);
                            Date nextExec = trigger.nextExecutionTime(triggerContext);
                            return nextExec;
                        }
                        return null;
                    }
                });
    }
}
